package cn.lyl.canting.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import cn.lyl.canting.R;
import cn.lyl.canting.bean.User;

//注册
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar toolbar;
    private EditText ed_1;
    private EditText ed_2;
    private EditText ed_3;
    private Button btn_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();

    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ed_1 = (EditText) findViewById(R.id.ed_1);
        ed_2 = (EditText) findViewById(R.id.ed_2);
        ed_3 = (EditText) findViewById(R.id.ed_3);
        btn_register = (Button) findViewById(R.id.btn_register);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//显示toolbar的返回图标
        getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏toolbar的自带的标题

        //给图标加点击事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                //点击注册进行验证
                    submit();
                break;
        }
    }

    private void submit() {
        // validate
        User user = new User();
        //获取用户输入的手机号
        String shoujihao = ed_1.getText().toString().trim();
        if (TextUtils.isEmpty(shoujihao)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }else {
            //将shoujihao存入user中
            user.setPhone(shoujihao);
        }
        //获取用户输入的密码
        String password_1 = ed_2.getText().toString().trim();
        if (TextUtils.isEmpty(password_1)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        //获取用户再次输入的密码
        String password_2 = ed_3.getText().toString().trim();
        if (TextUtils.isEmpty(password_2)) {
            Toast.makeText(this, "请再次输入密码", Toast.LENGTH_SHORT).show();
            return;
        }else{
            //判断两次密码输入是否一致
            if(password_1.equals(password_2) ){
                //将password_2存入user中
                user.setPassword(password_2);
            }else {
                Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
            }
        }
        //判断user中是否有数据
        if(user.getPhone()!=null&&user.getPassword()!=null){
            //获取数据库中User表中phone列
            List<User> users  = LitePal.select("phone").find(User.class);

            //将所有电话号码存入集合中
            List<String> phones = new ArrayList<String>();
            for (int i = 0; i < users.size(); i++) {
                phones.add(users.get(i).getPhone());
            }
            //判断phones集合中是否存在该电话号码
            if(phones.contains(user.getPhone())){
                Toast.makeText(this, "该用户已注册", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                //将user保存至User表中
                user.save();
                finish();
            }
        }
    }
}
